package com.lastproject.ejb.dao;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.xml.bind.DatatypeConverter;

import com.lastproject.dto.EmployeeDTO;
import com.lastproject.ejb.entities.Employee;
import com.lastproject.ejb.service.PasswordEncryption;
import com.lastproject.ejb.util.EmployeeConversion;

/**
 * Session Bean implementation class EmployeeDAO
 */
@Stateless
public class EmployeeDAO implements EmployeeDAORemote {

	@PersistenceContext
	EntityManager em;

	@EJB
	EmployeeConversion conv;

	/**
	 * Default constructor.
	 */
	public EmployeeDAO() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void create(EmployeeDTO employeeDTO) {
		// TODO Auto-generated method stub
		// initial password, represented as a string
		String tempPass = employeeDTO.getPassword();
		String password_encrypted2 = "";

		System.out.println("pass from gui: " + tempPass);

		try {

			// initial password after being hashed using SHA-1
			// represented as byte[]
			byte[] hashedPassword = new PasswordEncryption()
					.getEncryptedPassword(tempPass,
							new PasswordEncryption().getSalt());

			// hashed password stored as string
			// this version of the password will be stored in the db
			password_encrypted2 = DatatypeConverter
					.printHexBinary(hashedPassword);
			employeeDTO.setPassword(password_encrypted2);
		} catch (NoSuchAlgorithmException | InvalidKeySpecException
				| UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		em.persist(conv.fromDTO(employeeDTO));
	}

	@Override
	public void edit(EmployeeDTO employeeDTO) {
		// TODO Auto-generated method stub
		em.merge(conv.fromDTO(employeeDTO));
	}

	@Override
	public void remove(EmployeeDTO employeeDTO) {
		// TODO Auto-generated method stub
		em.remove(em.merge(conv.fromDTO(employeeDTO)));
	}

	@Override
	public EmployeeDTO findById(int id) {
		// TODO Auto-generated method stub
		Employee employee = new Employee();

		employee = em.find(Employee.class, id);

		return conv.fromEntity(employee);
	}

	@Override
	public List<EmployeeDTO> getAll() {
		// TODO Auto-generated method stub
		List<Employee> employeeList = new ArrayList<Employee>();

		employeeList = em.createNamedQuery("Employee.findAll", Employee.class)
				.getResultList();

		List<EmployeeDTO> eDTOList = new ArrayList<EmployeeDTO>();

		for (Employee e : employeeList) {
			eDTOList.add(conv.fromEntity(e));
		}

		return eDTOList;
	}

	// created by: erik
	@Override
	public List<EmployeeDTO> getTeamLeaders() {
		List<Employee> employeeList = new ArrayList<Employee>();

		employeeList = em.createQuery(
				"SELECT e FROM Employee e WHERE e.isTeamLeader = 1",
				Employee.class).getResultList();

		List<EmployeeDTO> eDTOList = new ArrayList<EmployeeDTO>();

		for (Employee e : employeeList) {
			eDTOList.add(conv.fromEntity(e));
		}

		return eDTOList;
	}

	// needs to be modified after new db!!!
	// created by: erik
	@Override
	public List<EmployeeDTO> getDepartmentManagers() {
		List<Employee> employeeList = new ArrayList<Employee>();

		employeeList = em.createQuery(
				"SELECT e FROM Employee e WHERE e.isDepartmentLeader = 1",	// isDepartmentManager  <---HERE
				Employee.class).getResultList();

		List<EmployeeDTO> eDTOList = new ArrayList<EmployeeDTO>();

		for (Employee e : employeeList) {
			eDTOList.add(conv.fromEntity(e));
		}

		return eDTOList;
	}

}
